package cn.iocoder.yudao.module.member.service.useranswer;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.useranswer.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.useranswer.UserAnswerDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.useranswer.UserAnswerMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 问答明细 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserAnswerServiceImpl implements UserAnswerService {

    @Resource
    private UserAnswerMapper userAnswerMapper;

    @Override
    public Long createUserAnswer(UserAnswerSaveReqVO createReqVO) {
        // 插入
        UserAnswerDO userAnswer = BeanUtils.toBean(createReqVO, UserAnswerDO.class);
        userAnswerMapper.insert(userAnswer);
        // 返回
        return userAnswer.getId();
    }

    @Override
    public void updateUserAnswer(UserAnswerSaveReqVO updateReqVO) {
        // 校验存在
        validateUserAnswerExists(updateReqVO.getId());
        // 更新
        UserAnswerDO updateObj = BeanUtils.toBean(updateReqVO, UserAnswerDO.class);
        userAnswerMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserAnswer(Long id) {
        // 校验存在
        validateUserAnswerExists(id);
        // 删除
        userAnswerMapper.deleteById(id);
    }

    private void validateUserAnswerExists(Long id) {
        if (userAnswerMapper.selectById(id) == null) {
            throw exception(USER_ANSWER_NOT_EXISTS);
        }
    }

    @Override
    public UserAnswerDO getUserAnswer(Long id) {
        return userAnswerMapper.selectById(id);
    }

    @Override
    public PageResult<UserAnswerDO> getUserAnswerPage(UserAnswerPageReqVO pageReqVO) {
        return userAnswerMapper.selectPage(pageReqVO);
    }

}